//
//  NSString+FIDExtra.m
//  StringExtra
//
//  Created by Fidetro on 2017/6/29.
//  Copyright © 2017年 Fidetro. All rights reserved.
//

#import "NSString+FIDExtra.h"

@implementation NSString (FIDExtra)

- (CGSize)sizeWithTextMaxSize:(CGSize)textMaxSize font:(UIFont *)font
{
    CGSize textSize = CGSizeZero;
    
    if (self.length > 0)
    {
        NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
        paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
        
        NSDictionary *attributesDic = @{NSFontAttributeName: font,
                                        NSParagraphStyleAttributeName: paragraphStyle};
        
        CGRect textRect = [self boundingRectWithSize:textMaxSize
                                             options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
                                          attributes:attributesDic
                                             context:nil];
        
        textSize = CGSizeMake(CGRectGetWidth(textRect), ceilf(CGRectGetHeight(textRect)));
    }
    
    return textSize;
}

- (NSString *(^)(NSUInteger fromIndex))fromIndex
{
    return ^(NSUInteger index){
        return [self substringWithRange:NSMakeRange(index, self.length - index)];
    };
}

- (NSString *(^)(NSUInteger toIndex))toIndex
{
    return ^(NSUInteger index){
        return [self substringWithRange:NSMakeRange(0, index)];
    };
}

- (NSString *(^)(NSUInteger beginIndex,NSUInteger endIndex))subString
{
    return ^(NSUInteger beginIndex,NSUInteger endIndex){
        return [self substringWithRange:NSMakeRange(beginIndex, endIndex-beginIndex)];
    };
}
@end
